package com.example.config.securityConfig.authAction;

import com.alibaba.fastjson.JSON;
import com.example.common.utils.ApiResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class UrlAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("utf-8");    //设置 HttpServletResponse使用utf-8编码
        httpServletResponse.setHeader("Content-Type", "text/html;charset=utf-8");  //设置响应头的编码
        ApiResult apiResult = new ApiResult(Boolean.FALSE);
        apiResult.setData(403);
        apiResult.setMessage("权限不足");
        httpServletResponse.getWriter().write(JSON.toJSON(apiResult).toString());
    }
}